﻿/**************************************************************
* Copyright (C) 2022 www.eipflow.com 孙泽伟版权所有(盗版必究)
*
* 作者: 孙泽伟(QQ 1039318332)
* 创建时间: 2022/01/12 22:40:15
* 文件名: 
* 描述: 
* 
* 修改历史
* 修改人：
* 时间：
* 修改说明：
*
**************************************************************/
namespace EIP.System.Logic.Impl
{
    /// <summary>
    /// 系统配置文件接口实现
    /// </summary>
    public class SystemDataBaseLogic : ISystemDataBaseLogic
    {
        #region 构造函数

        /// <summary>
        /// filterSql字段，是否过滤数据字段
        /// </summary>
        private readonly string[] _filterSqlField = { "switch", "sign", "editor" };

        /// <summary>
        /// 数据库连接
        /// </summary>
        private readonly SystemDataBaseInput dataBase = new SystemDataBaseInput();

        private readonly ISystemDataBaseRepository _dataBaseRepository;
        private readonly ISystemZeroCodeLogic _zeroCodeLogic;
        private readonly ISystemPermissionLogic _permissionLogic;
        private readonly ICapPublisher _publisher;
        private readonly ISystemSerialNoLogic _systemSerialNoLogic;
        /// <summary>
        /// 
        /// </summary>
        public SystemDataBaseLogic(ISystemSerialNoLogic systemSerialnoLogic, ICapPublisher publisher, ISystemDataBaseRepository dataBaseRepository,
            ISystemZeroCodeLogic zeroCodeLogic, ISystemPermissionLogic permissionLogic)
        {
            _systemSerialNoLogic = systemSerialnoLogic;
            _publisher = publisher;
            _dataBaseRepository = dataBaseRepository;
            _zeroCodeLogic = zeroCodeLogic;
            _permissionLogic = permissionLogic;
        }


        #endregion

        public async Task<OperateStatus<IEnumerable<BaseTree>>> FindTableTree()
        {
            List<BaseTree> jsTree = new List<BaseTree>();

            //循环表
            var tables = (await FindDataBaseTable(new IdInput { })).Data.ToList();
            foreach (var table in tables)
            {
                jsTree.Add(new BaseTree
                {
                    parent = Guid.Empty,
                    text = table.Name + (table.Description.IsNotNullOrEmpty() ? "-[" + table.Description + "]" : ""),
                    id = table.Name,
                    icon = "table"
                });
            }

            //获取字段
            var columns = (await FindDataBaseColumns(new SystemDataBaseTableDto()
            {
                Name = tables.Select(s => s.Name).ExpandAndToString()
            })).Data.ToList();
            foreach (var table in tables)
            {
                foreach (var column in columns.Where(w => w.TableName == table.Name))
                {
                    jsTree.Add(new BaseTree
                    {
                        parent = table.Name,
                        text = column.Name + "(" + column.DataType + (column.DataType.Contains("char") ? "(" + column.MaxLength + ")" : "") + "," + (column.IsNullable.ToLower() == "yes" ? "null" : "not null") + ")"/* +(column.ColumnDescription.IsNullOrEmpty()?"": "-[" + column.ColumnDescription + "]")*/ ,
                        id = table.Name + "|" + column.Name,
                        icon = "bars"
                    });
                }
            }
            return OperateStatus<IEnumerable<BaseTree>>.Success(jsTree);
        }

        /// <summary>
        /// 查看对应数据库空间占用情况
        /// </summary>
        /// <returns></returns>
        public async Task<OperateStatus<IEnumerable<SystemDataBaseSpaceOutput>>> FindDataBaseSpaceused()
        {
            return OperateStatus<IEnumerable<SystemDataBaseSpaceOutput>>.Success(await _dataBaseRepository.FindDataBaseSpaceused());
        }

        /// <summary>
        /// 获取对应数据库表信息
        /// </summary>
        /// <returns></returns>
        public async Task<OperateStatus<IEnumerable<SystemDataBaseTableDto>>> FindDataBaseTable(IdInput input)
        {
            var tables = await _dataBaseRepository.FindDataBaseTable(dataBase);
            using (var fix = new SqlDatabaseFixture())
            {
                var zeroCode = await fix.Db.SystemZeroCode.SetSelect(s => new { s.TableName }).FindAllAsync(f => f.Type == 2);
                foreach (var item in zeroCode.Where(w => w.TableName.IsNotNullOrEmpty()))
                {
                    var zeroCodeData = tables.Where(a => a.Name.ToLower().Contains(item.TableName.ToLower()));
                    foreach (var data in zeroCodeData)
                    {
                        data.IsFromZeroCode = true;
                    }
                }
            }
            return OperateStatus<IEnumerable<SystemDataBaseTableDto>>.Success(tables.OrderByDescending(o => o.IsFromZeroCode));
        }

        /// <summary>
        /// 获取对应数据库表信息
        /// </summary>
        /// <returns></returns>
        public async Task<OperateStatus<IEnumerable<SystemDataBaseTableDto>>> FindDataBaseView(IdInput input)
        {
            return OperateStatus<IEnumerable<SystemDataBaseTableDto>>.Success(await _dataBaseRepository.FindDataBaseView(dataBase));
        }

        /// <summary>
        /// 获取对应数据库表信息
        /// </summary>
        /// <returns></returns>
        public async Task<OperateStatus<IEnumerable<SystemDataBaseTableDto>>> FindDataBaseProc(IdInput input)
        {
            return OperateStatus<IEnumerable<SystemDataBaseTableDto>>.Success(await _dataBaseRepository.FindDataBaseProc(dataBase));
        }
        /// <summary>
        /// 获取对应表列信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<OperateStatus<IEnumerable<SystemDataBaseColumnDto>>> FindDataBaseColumns(SystemDataBaseTableDto input)
        {
            return OperateStatus<IEnumerable<SystemDataBaseColumnDto>>.Success(await _dataBaseRepository.FindDataBaseColumns(input));
        }
    }
}